// crypt.h: interface for the CLCrypt class.
// C++ Wraper for DES/3DES
// $Id: crypt.h,v 1.1 2002/09/26 01:53:59 leon Exp $
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CRYPT_H__9484F28D_44B1_46A7_AA35_68EA4F879E0F__INCLUDED_)
#define AFX_CRYPT_H__9484F28D_44B1_46A7_AA35_68EA4F879E0F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "str.h"
#include "des.h"

#define CLCRYPT_ENCRYPT	0
#define CLCRYPT_DECRYPT	1

class CLCrypt  
{
public:
	int parallel(const CLStr& ser,CLStrList& list);
	void serialize(const CLStrList& list,CLStr& ser);
	static void debase64(const CLStr& from,CLStr& to);
	static void enbase64(const CLStr& from,CLStr& to);
	static int split(const CLStr& from,CLStrList& list);
	static void join(const CLStrList& list,CLStr& to);
	int encrypt(const CLStr& from,CLStr& to) { return crypt(from,to); };
	int decrypt(const CLStr& from,CLStr& to) {return crypt(from,to,CLCRYPT_DECRYPT); };
	int crypt(const CLStr& from,CLStr& to,int mode=CLCRYPT_ENCRYPT);
	virtual int block_crypt(LPCBYTE from,LPBYTE to,int mode=CLCRYPT_ENCRYPT) { return 0;};
	CLCrypt() {};
	virtual ~CLCrypt() {};
};

class CLDES: public CLCrypt
{
public:
	struct _des_ctx	m_context;
	CLDES() {};
	CLDES(LPCBYTE key) { init(key); };
	virtual ~CLDES() {};
	int block_crypt(LPCBYTE from,LPBYTE to,int mode=CLCRYPT_ENCRYPT);
	void init(LPCBYTE key) { cldes_setkey(&m_context,key); };
};


class CL3DES: public CLCrypt
{
public:
	void init(LPCBYTE key,int len);
	struct _tripledes_ctx m_context;
	CL3DES() {};
	CL3DES(LPCBYTE key,int len) { init(key,len); };
	virtual ~CL3DES() {};
	int block_crypt(LPCBYTE from,LPBYTE to,int mode=CLCRYPT_ENCRYPT);
	void init(LPCBYTE key1,LPCBYTE key2,LPCBYTE key3=0) { 
		if(key3) tripledes_set3keys(&m_context,key1,key2,key3);
		else tripledes_set2keys(&m_context,key1,key2);
	}
};

#endif // !defined(AFX_CRYPT_H__9484F28D_44B1_46A7_AA35_68EA4F879E0F__INCLUDED_)
